(function() {
	if (!window.webcool) {
		webcool = {
			createXmlHttpObject : function () {
				var xmlHttp=null;
				try {
					xmlHttp = new XMLHttpRequest();
				} catch (e) {
					try {
						xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
					} catch (e) {
						xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
					}
				}
				
				return xmlHttp;
			},

			_getFormData : function(formElement) {
				var data = "";
				for(var i=0,count=formElement.childNodes.length; i<count; i++) {
					var child = formElement.childNodes[i];
					var childTagName = ( child.tagName != null )? child.tagName.toLowerCase() : "";
					switch( childTagName ) {
						case "input" :
							if( child.type == "text"
								|| child.type == "hidden"
								|| (child.type == "radio" && child.checked) ) {
								data += "&" + child.name + "=" + child.value;
							} else if( child.type == "checkbox" ) {
								data += "&" + child.name + "=" + ((child.checked)? encodeURIComponent(child.value) : "");
							}
							break;

						case "select" :
							data += "&" + child.name + "=" + encodeURIComponent((child.selectedIndex>-1)? child.options[child.selectedIndex] : "");
							break;
					}
				}
				
				return data;
			},
			
			sendRequest : function(cbKey, hashKey, parameters,
				loadingHandler, successHandler, successHandlerArgs, failureHandler, failureHandlerArgs, formElement) {
				var xmlHttp = webcool.createXmlHttpObject();
				if( xmlHttp != null ) {
					var data = cbKey + "=" + encodeURIComponent(hashKey);
					if( parameters != null ) {
						data += "&" + parameters;
					}
					
					if( formElement != null
						&& formElement.tagName.toUpperCase() == "FORM" ) {
						data += "&" + this._getFormData(formElement);
					}
					
					var targetElement = document.getElementById(hashKey);
					xmlHttp.onreadystatechange = function() {
						if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
							if (xmlHttp.status == 200) {
								if (xmlHttp.responseText.length > 0) {
									targetElement.innerHTML = xmlHttp.responseText;
								}
								
								if( successHandler != null ) {
									successHandler(targetElement, successHandlerArgs);
								}
							} else {
								if( failureHandler != null ) {
									failureHandler(targetElement, failureHandlerArgs);
								}
							}
						} else {
							if( loadingHandler != null ) {
								loadingHandler(targetElement);
							}
						}
					};

					xmlHttp.open("POST", window.location, true);
					xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded;charset=UTF-8");	// multipart/form-data
					xmlHttp.setRequestHeader("Content-length", data.length);
					xmlHttp.setRequestHeader("Connection", "close");
					
					xmlHttp.send(data);
				}
				
				return false;
			}

		};
	}	
})();